package com.zqs.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zqs.bean.StudentDO;
import com.zqs.common.R;
import com.zqs.common.RUtil;
import com.zqs.controller.vo.StudentEditParam;
import com.zqs.mapper.StudentMapper;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 学生管理
 *
 * @author zhangqingshan
 * @date 2024/03/31
 */
@RequestMapping("/student")
@AllArgsConstructor
@RestController
public class StudentController {

    final StudentMapper studentMapper;

    @GetMapping("/list/{classNum}")
    public R list(@PathVariable("classNum") Integer classNum) {
        return RUtil.ok(studentMapper.selectList(Wrappers.lambdaQuery(StudentDO.class)
                .eq(StudentDO::getClassNum, classNum)));
    }

    @PostMapping("/edit")
    @Transactional
    public R edit(@RequestBody @Validated StudentEditParam param) {
        StudentDO studentDO = new StudentDO();
        studentDO.setStuName(param.getStuName());
        return RUtil.ok(studentMapper.update(studentDO, Wrappers.lambdaQuery(StudentDO.class)
                .eq(StudentDO::getClassNum, param.getClassNum())
                .eq(StudentDO::getStuNum, param.getStuNum())));
    }
}
